As mentioned in the previous example, multiple models can be deleted in a batch. Here's a quick example.
'use strict';
const MySQLDriver = require('node-data-mapper-mysql').MySQLDriver;
const driver = new MySQLDriver(require('../bikeShopConOpts.json'));
driver
.initialize()
.then(runQuery)
.then(printResult)
.catch(console.error)
.finally(() => driver.end());
function runQuery(dataContext) {
// Delete a bonus record and two staff records by ID.
const query = dataContext
.delete({
bonuses: {
bonusID: 3
},
staff: [
{
staffID: 1
},
{
staffID: 3
}
]
});
console.log('Query:');
console.log(query.toString(), '\n');
return query
.execute();
}
function printResult(result) {
console.log('Result:');
console.log(result);
}
Running this example ($ node example/delete/deleteMultipleModels.js) logs the following output, which accurately reflects the number of affected (deleted) rows.
Query:
DELETE `bonuses`
FROM `bonuses` AS `bonuses`
WHERE (`bonuses`.`bonusID` = :bonuses_bonusID_0);
DELETE `staff`
FROM `staff` AS `staff`
WHERE (`staff`.`staffID` = :staff_staffID_0);
DELETE `staff`
FROM `staff` AS `staff`
WHERE (`staff`.`staffID` = :staff_staffID_0)
Result:
{ affectedRows: 3 }